package com.awesome.uidemo.customview.customview.slide;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.awesome.common.util.device.ScreenUtil;
import com.awesome.common.util.log.ShowLogUtil;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/3/22 10:57 下午
 * Description：滑块
 */
public class SlideView extends LinearLayout {

    private int mScreenHeight;

    public SlideView(Context context) {
        super(context);
        initView();
    }

    public SlideView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SlideView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mScreenHeight = ScreenUtil.getScreenHeight(this.getContext());
    }

    int lastY = 0;
    int lastRawY = 0;
    int lastLeft = 0;
    int lastTop = 0;
    int lastRight = 0;
    int lastBottom = 0;
    int offsetMaxY = 0;
    int offsetRawY = 0;
    boolean topSlideEnable = true;
    boolean bottomSlideEnable = true;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        int rawY = (int) event.getRawY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = y;
                lastRawY = rawY;
                lastLeft = getLeft();
                lastTop = getTop();
                lastRight = getRight();
                lastBottom = getBottom();
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetY = y - lastY;
                offsetRawY = rawY - lastRawY;
                if (mOnScrollListener != null)
                    mOnScrollListener.onScrollChange(offsetRawY);
                layout(getLeft(), getTop() + offsetY, getRight(), getBottom() + offsetY);
                break;
            case MotionEvent.ACTION_UP:
                if (offsetMaxY > 0) {
                    if (offsetRawY < 0) {
                        // 上滑
                        bottomSlideEnable = true;
                        if (!topSlideEnable){
                            layout(lastLeft, lastTop, lastRight, lastBottom);
                            break;
                        }
                        if (offsetMaxY > Math.abs(offsetRawY)) {
                            layout(lastLeft, lastTop - Math.abs(offsetRawY), lastRight, lastBottom - Math.abs(offsetRawY));
                            offsetMaxY -= Math.abs(offsetRawY);
                        } else {
                            // 超过最大值
                            layout(lastLeft, lastTop - offsetMaxY, lastRight, lastBottom - offsetMaxY);
                            topSlideEnable = false;
                        }
                    } else {
                        // 下滑
                        topSlideEnable = true;
                        if (!bottomSlideEnable){
                            layout(lastLeft, lastTop, lastRight, lastBottom);
                            break;
                        }
                        if (lastTop < 0) {
                            // 滑出上方屏幕，需要回归位置
                            if (Math.abs(lastTop)>offsetRawY){
                                layout(lastLeft, lastTop + Math.abs(offsetRawY), lastRight, lastBottom + Math.abs(offsetRawY));
                            }else {
                                layout(lastLeft, lastTop + Math.abs(lastTop), lastRight, lastBottom + Math.abs(lastTop));
                                bottomSlideEnable = false;
                            }
                        } else {
                            layout(lastLeft, lastTop, lastRight, lastBottom);
                        }
                    }
                } else {
                    layout(lastLeft, lastTop, lastRight, lastBottom);
                }
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        offsetMaxY = height - mScreenHeight;
    }

    private OnScrollListener mOnScrollListener;
    public void setOnScrollListener(OnScrollListener listener){
        mOnScrollListener = listener;
    }

    public interface OnScrollListener{
        void onScrollChange(int offsetRawY);
    }
}